package com.java.controller;

import com.java.model.Permission;
import com.java.model.Result;
import com.java.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 角色页面
     * @return
     */
    @GetMapping("/list")
    public String list() {
        return "/permission/list";
    }

    /**
     * 角色查询
     * @param permission
     * @return
     */
    @PostMapping("/toList")
    @ResponseBody
    public Result toList(Permission permission) {
        return permissionService.toList(permission);
    }

    /**
     * 角色新增页面
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return "permission/add";
    }

    /**
     * 新增角色
     * @param permission
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.success();
    }

    /**
     * 角色修改页面
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/edit")
    public String edit(int id, Model model) {
        // 获取id
        Result result = permissionService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("permission", result.getData());
        }
        return "permission/edit";
    }

    /**
     * 角色修改
     * @param permission
     * @return
     */
    @PostMapping("/modify")
    @ResponseBody
    public Result modify(@RequestBody Permission permission) {
        return permissionService.modify(permission);
    }

    /**
     * 删除角色信息
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(int[] ids){
        return permissionService.remove(ids);
    }

}
